package com.yinxianren.main.study.httpTest;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.util.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Map;
import java.util.TreeMap;

public class Test {


//    public static void main(String[] args) throws Exception {
//        String tradeInfo="{\"terminalMerName\":\"黃三\",\"signMsg\":\"sdgd2\",\"returnUrl\":\"sioifhio\",\"noticeUrl\":\"http://\",\"payFee\":\"5\",\"backFee\":\"8\",\"terminalMerId\":\"M201002-1127\",\"bizType\":\"3\",\"charset\":\"utf-8\",\"signType\":\"MD5\",\"merId\":\"M201001\",\"merOrderId\":\"2019ZX193\",\"bankCardName\":\"黃三\",\"userShortName\":\"黃三\",\"identityType\":\"1\",\"identityNum\":\"350425198606776667\",\"bankcardType\":\"1\",\"bankCardNum\":\"6216600100071122667\",\"bankCardPhone\":\"15117007672\",\"province\":\"450000\",\"city\":\"450500\",\"address\":\"北京香山口\",\"amount\":\"20\",\"smsCode\":\"111111\",\"bankCode\":\"123\",\"validDate\":\"321\",\"securityCode\":\"213\",\"backCardPhone\":\"12345678676\",\"backBankCode\":\"ABC\",\"backCardNum\":\"16895644561677\",\"bankCardType\":\"2\",\"originalMerOrderId\":\"moi201906210007\",\"cizType\":\"EX1001\",\"merchantType\":\"00\",\"terminalMerName\":\"黃三\",\"phone\":\"123165464\",\"bankaccProp\":\"0\",\"cardHolderName\":\"黃三\"}";
//        //加密
//        String encode = URLEncoder.encode(tradeInfo, "UTF-8");
//        String encodeANdBase64= new String(Base64.getEncoder().encode(encode.getBytes()));
//        System.out.println("报文加密后："+encodeANdBase64);
//
//        //解密
//        String tradeInfoDecode = new String(Base64.getDecoder().decode(encodeANdBase64.getBytes()));
//        String decode = URLDecoder.decode(tradeInfoDecode,"UTF-8");
//        System.out.println("报文解密后："+decode);
//    }


//    String result=HttpClientUtils.doPostJson(HttpClientUtils.getHttpsClient(),
//            extraChannelInfo.getUrl(),
//            JsonUtils.objectToJsonNonNull(squareTrade));

    public static void main(String[] args) throws Exception {
        testAddCusInfo();
    }



    public static void testAddCusInfo() throws UnsupportedEncodingException {
        //数据报文
        String tradeInfo="{\n" +
                "    \"terminalMerName\": \"黃酒仙\",\n" +
                "    \"signMsg\": \"sdgd2\",\n" +
                "    \"returnUrl\": \"sioifhio\",\n" +
                "    \"noticeUrl\": \"http://\",\n" +
                "    \"payFee\": \"5\",\n" +
                "    \"backFee\": \"8\",\n" +
                "    \"terminalMerId\": \"M201002-1125\",\n" +
                "    \"bizType\": \"3\",\n" +
                "    \"charset\": \"utf-8\",\n" +
                "    \"signType\": \"MD5\",\n" +
                "    \"merId\": \"M201001\",\n" +
                "    \"merOrderId\": \"2019ZX191\",\n" +
                "    \"bankCardName\": \"黃酒仙\",\n" +
                "    \"userShortName\": \"黃酒仙\",\n" +
                "    \"identityType\": \"1\",\n" +
                "    \"identityNum\": \"350425198606776667\",\n" +
                "    \"bankcardType\": \"1\",\n" +
                "    \"bankCardNum\": \"6216600100071122667\",\n" +
                "    \"bankCardPhone\": \"15117007672\",\n" +
                "    \"province\": \"450000\",\n" +
                "    \"city\": \"450500\",\n" +
                "    \"address\": \"北京香山口\",\n" +
                "    \"amount\": \"20\",\n" +
                "    \"smsCode\": \"111111\",\n" +
                "    \"bankCode\": \"123\",\n" +
                "    \"validDate\": \"321\",\n" +
                "    \"securityCode\": \"213\",\n" +
                "    \"backCardPhone\": \"12345678676\",\n" +
                "    \"backBankCode\": \"ABC\",\n" +
                "    \"backCardNum\": \"16895644561677\",\n" +
                "    \"currency\": \"CNY\",\n" +
                "    \"originalMerOrderId\": \"moi201906210007\",\n" +
                "    \"cizType\": \"EX1001\",\n" +
                "    \"merchantType\": \"00\",\n" +
                "    \"phone\": \"123165464\",\n" +
                "    \"bankaccProp\": \"0\"\n" +
                "}";//key
        String md5Key="AVhPxPG9";
        //原始数据 map
        Map<String, Object> tradeInfoMap = JsonUtils.jsonToMap(tradeInfo);
        //排序数据map
        Map<String, Object> linkedMap=new TreeMap<>();
        linkedMap.putAll(tradeInfoMap);
        //签名
        StringBuffer sb = new StringBuffer();
        for (String key : linkedMap.keySet()){
            String value = linkedMap.get(key) != null ? linkedMap.get(key).toString() : null;
            if(!StringUtils.isEmpty(value)){
                sb.append(key+"="+value+"&");
            }
        }
        String sign = sb.substring(0,sb.lastIndexOf("&"))+md5Key;
        System.out.println("sign:"+sign);
        String md5Sign=  DigestUtils.md5Hex(sign.getBytes(StandardCharsets.UTF_8)).toUpperCase();
        //放入sign
        tradeInfoMap.put("sign",md5Sign);
        //转Jsom
        String msg=JsonUtils.objectToJsonNonNull(tradeInfoMap);
        System.out.println("msg："+msg);
        //加密
        String encode = URLEncoder.encode(msg, "UTF-8");
        String encodeANdBase64= new String(Base64.getEncoder().encode(encode.getBytes()));
        System.out.println("报文加密后："+encodeANdBase64);

//        String result=HttpClientUtils.doPostJson(HttpClientUtils.getHttpsClient(),
//           "",
//                encodeANdBase64);

    }


}
